﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EM.Repository.utility;
using System.Data;
using System.Data.SqlClient;

namespace EM.definition.finance.BankAccount
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Authentication ua = new Authentication();
            ua.CheckRolePageAuthentication(12, Authentication.RedirectionURLTypes.LoginPage);

            if (!IsPostBack)
            {
                FillCombo fc = new FillCombo();
                fc.Bank(ddlBANK, true, 0, false);
                fc.LookUpValue(ddlACTIVE, "", true, "SYS_YES_NO", Session["LANGCODE"].ToString());
            }

        }
        private DataTable GetData()
        {
            SqlCommand cmd = new SqlCommand();
            string sSQL = "";
            sSQL += @"SELECT A.*, U.USERDESC FROM 
                        (SELECT BANKACCOUNTID, B.BANKID, B.BANKNAME, BANKACCOUNTNAME,IBAN,ACCOUNTNO, CASE WHEN ISSALARY = 'Y' THEN 'Evet' ELSE 'Hayır' END AS ISSALARY,USERID,
                    CASE WHEN BA.ACTIVEFLG = 'Y' THEN 'Evet' ELSE 'Hayır' END AS ACTIVEFLG FROM TBLBANKACCOUNT BA, TBLBANK B
                    WHERE BA.BANKID = B.BANKID";
            if (txtBANKACCOUNTNAME.Text != "")
            {
                sSQL += "  AND BANKACCOUNTNAME LIKE @BANKACCOUNTNAME ";
                cmd.Parameters.AddWithValue("@BANKACCOUNTNAME", "%" + txtBANKACCOUNTNAME.Text + "%");
            }
            if (ddlACTIVE.SelectedValue != "")
            {
                sSQL += "  AND BA.ACTIVEFLG = @ACTIVEFLG ";
                cmd.Parameters.AddWithValue("@ACTIVEFLG", ddlACTIVE.SelectedValue);
            }
            if (ddlBANK.SelectedValue != "0")
            {
                sSQL += "  AND B.BANKID = @BANKID ";
                cmd.Parameters.AddWithValue("@BANKID", ddlBANK.SelectedValue);
            }
            sSQL += " AND BA.WORKGROUPID = @WORKGROUPID ";
            cmd.Parameters.AddWithValue("@WORKGROUPID", Session["WORKGROUPID"]);
            sSQL += " ) AS A LEFT OUTER JOIN TBLUSER U ON A.USERID = U.USERID";
            sSQL += "    ORDER BY BANKACCOUNTNAME";
           

            cmd.CommandText = sSQL;

            DataTable dt = new DataTable();
            DbHelper dsrv = new DbHelper();

            dt = dsrv.GetDataTable(cmd, "TBL");
            return dt;
        }

        protected void rgMaster_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
        {
            DataTable dt = GetData();
            rgMaster.DataSource = dt;

            rgMaster.MasterTableView.NoMasterRecordsText = "Kayıt bulunamadı.";
            rgMaster.StatusBarSettings.ReadyText = dt.Rows.Count + " adet kayıt bulundu.";
        }

        protected void rgMaster_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            { 
                
            }
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Pager)
            {
                Label lblRecordCount = new Label();
                e.Item.Controls[0].Controls.AddAt(0, lblRecordCount);
                lblRecordCount.Text = rgMaster.StatusBarSettings.ReadyText + " | sayfaya git : ";

                rgMaster.ShowStatusBar = rgMaster.PageCount <= 1 ? true : false;
            }
        }

        protected override void RaisePostBackEvent(IPostBackEventHandler source, String eventArgument)
        {
            base.RaisePostBackEvent(source, eventArgument);
            switch (eventArgument)
            {
                case "Rebind":
                    rgMaster.Rebind();
                    break;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            rgMaster.Rebind();

        }
    }
}